class Category < ActiveRecord::Base
  belongs_to :parent_category, :class_name => 'Category', :foreign_key => 'parent_id'
  has_many :sub_categories, :class_name => 'Category', :foreign_key => 'parent_id', :order => 'position'
  acts_as_list :scope => :parent_id

  #二级分类
  has_and_belongs_to_many :two_shops, :join_table => "categories_shops", :foreign_key => "category_id"
  #一级分类
  has_many :shops

  def top_shops(num = 10)
    if id == 857
      shops.order('total_sales DESC').limit(num)
    else
      shops.where("prom_url is not null and prom_url <> 'bad'").order('total_sales DESC').limit(num)
    end
  end

  def self.all_one_level
    where(:level => 1).sort{|a, b|b.shops.count <=> a.shops.count}
  end
end
